package com.dongdong.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.dongdong.pojo.Cart;
import com.dongdong.pojo.TbItem;
import com.dongdong.pojo.TbOrderItem;
import com.dongdong.service.CartService;
import com.dongdong.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @Deacription TODO
 * @Author Lenovo
 * @Date 2020/12/21 18:01
 * @Version 1.0
 **/
@Component
@Service
public class CartServiceImpl implements CartService {

    @Reference
    private ItemService itemService;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public List<Cart> addCart(List<Cart> cartList, Long itemId, Integer num) {
        //cartList已经存在的购物车,itemId要添加的商品 num要添加的商品的数量
        //第一种可能性:购物车没有数据,或者购物有数据但是没有在当前店铺(商家)买东西
        //第二种可能性:购物车有数据,也有这个店铺的东西,但是没有这件商品
        //第三种可能性:购物车有数据,也有店铺的东西,也买过这件商品

        //1.根据传入的itemId查询商品信息
        TbItem item = itemService.findItemById(itemId);
        //2.拿到商家的id
        String sellerId = item.getSellerId();
        //3.通过商家的id去判断carList之前有没有买过这家店铺的东西
        Cart cart = getCartBySellerId(cartList,sellerId);
        //4.如果没买过
        if (cart == null){
            //4.1创建购物车对象cart
            cart = new Cart();
            //4.2将数据放到购物车
            cart.setSellerId(sellerId);
            cart.setSellerName(item.getSeller());
            List<TbOrderItem> orderItemList = new ArrayList<>();
            TbOrderItem orderItem = new TbOrderItem();
            orderItem.setNum(num);
            orderItem.setSellerId(sellerId);
            orderItem.setPrice(item.getPrice());
            orderItem.setPicPath(item.getImage());
            orderItem.setGoodsId(item.getGoodsId());
            orderItem.setTotalFee(new BigDecimal(item.getPrice().doubleValue() * num ));
            orderItem.setTitle(item.getTitle());
            orderItem.setItemId(item.getId());
            orderItemList.add(orderItem);
            cart.setOrderItemList(orderItemList);
            //将新创建的购物车放到购物车列表中
            cartList.add(cart);
        }else {
            //5.如果买过这家店铺的东西,也就sellerId判断不为null
            List<TbOrderItem> orderItemList = cart.getOrderItemList();
            //6.购物明细车列表中是否有该商品
            TbOrderItem orderItem = getTbOrderItemByItemId(orderItemList,itemId);
            //6.1如果没有,list添加
            if (orderItem == null){
                orderItem = new TbOrderItem();
                orderItem.setNum(num);
                orderItem.setSellerId(sellerId);
                orderItem.setPrice(item.getPrice());
                orderItem.setPicPath(item.getImage());
                orderItem.setGoodsId(item.getGoodsId());
                orderItem.setTotalFee(new BigDecimal(item.getPrice().doubleValue() * num ));
                orderItem.setTitle(item.getTitle());
                orderItem.setItemId(item.getId());
                orderItemList.add(orderItem);
            }else {
                //6.2如果有,在原来的明细对象上修改数据和总价格
                orderItem.setNum(orderItem.getNum() + num);
                orderItem.setTotalFee(new BigDecimal(orderItem.getNum() * orderItem.getPrice().doubleValue()));
                //如果当前商品购买数量小于或者等于,就说明用户没有买
                if (orderItem.getNum() <= 0){
                    //就将此商品删除
                    orderItemList.remove(orderItem);
                }
                //如果删除掉商品之后的明细列表为空了,我们就认为当前当铺没有添加到购物车,因此要将当前店铺的购物车从购物车列表中移除
                if (orderItemList.size() <= 0){
                    cartList.remove(cart);
                }
            }

        }
        return cartList;
    }

    @Override
    public List<Cart> findCartListFromRedis(String userId) {
        List<Cart> cartList = (List<Cart>)redisTemplate.boundHashOps("cartList").get(userId);
        if (cartList == null){
            cartList = new ArrayList<>();
        }
        return cartList;
    }

    @Override
    public void saveCartListToRedis(String userId, List<Cart> cartList) {
        redisTemplate.boundHashOps("cartList").put(userId,cartList);
    }

    @Override
    public List<Cart> mergeCartListCookieAndRedis(List<Cart> cartListCookie, List<Cart> cartListRedis) {
        for (Cart cartCookie : cartListCookie) {
            for (TbOrderItem orderItem : cartCookie.getOrderItemList()){
                cartListRedis = addCart(cartListRedis,orderItem.getItemId(),orderItem.getNum());
            }
        }
        return cartListRedis;
    }

    //根据sellerid去原购物车列表中查询购物车数据
    private Cart getCartBySellerId(List<Cart> cartList,String sellerId){
        for (Cart cart : cartList) {
            if (Objects.equals(cart.getSellerId(),sellerId)){
                return cart;
            }
        }
        return null;
    }


    //根据itemId查询购物车明细列表中是否有这件商品
    private TbOrderItem getTbOrderItemByItemId(List<TbOrderItem> orderItemList, Long itemId){
        for (TbOrderItem orderItem : orderItemList) {
            if (Objects.equals(itemId,orderItem.getItemId())){
                //之前购物车中有这件商品
                return orderItem;
            }
        }
        return null;
    }

}

